﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Web.Mvc;
//using www.SService;

namespace www.Controllers
{
    public class AdvertiseControllers : Controller
    {
        private readonly ObjectCache _cache = MemoryCache.Default;
        //private readonly ServiceClient _service1 = new ServiceClient();
        //private readonly Utility _utility=new Utility();
        //private static List<SelectListItem> _listItemsList;
        ////
        //// GET: /Advertise/
        //[HttpGet]
        //public ActionResult Index()
        //{
        //    try
        //    {
        //        if (_cache["Advertise"] == null)
        //        {

        //            var lData = _service1.GetAdvertiseDataContracts();
        //            if (lData.Any())
        //            {
        //                var policy = new CacheItemPolicy {AbsoluteExpiration = DateTimeOffset.Now.AddHours(27)};
        //                _cache.Set("Advertise", lData, policy);
        //                ViewData["Data"] = lData.Where(p => p.Type == 1).ToList();
        //            }
        //        }
        //        else
        //        {
        //            var lData = (List<RelaxAdvertiseDataContract>)_cache["Advertise"];
        //            ViewData["Data"] = lData.Where(p => p.Type == 1).ToList();

        //        }
        //        LoadAdvertiseType();

        //        return View();
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("<script>alert('chi tiết lỗi:" + ex.Message + "');</script>");
        //        return View();
        //    }
        //}
        //[HttpPost]
        //public ActionResult Index(FormCollection collection)
        //{
        //    try
        //    {
        //        var type = collection["Type"];
        //        LoadAdvertiseType(Convert.ToInt32(type));

        //        if (_cache["Advertise"] != null)
        //        {
        //            var lReturn =
        //                ((IEnumerable<RelaxAdvertiseDataContract>)_cache["Advertise"]).Where(
        //                    p => p.Type == Convert.ToInt32(type)).ToList();

        //            ViewData["Data"] = lReturn;
        //        }
        //        else
        //        {
        //            var lData = _service1.GetAdvertiseDataContracts();
        //            if (lData.Any())
        //            {
        //                var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
        //                _cache.Set("Advertise", lData, policy);
        //                ViewData["Data"] = lData.Where(p => p.Type == Convert.ToInt32(type)).ToList();
        //            }
        //        }
        //        return View();
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("<script>alert('chi tiết lỗi:" + ex.Message + "');</script>");
        //        return View();
        //    }
        //}

        //private void LoadAdvertiseType(int id=0)
        //{
        //    if (id == 0)
        //    {
        //           var listItemsList = new List<SelectListItem>
        //           {
        //               new SelectListItem {Text = "Logo", Value = "1", Selected = true},
        //               new SelectListItem {Text = "Banner", Value = "2"},
        //               new SelectListItem {Text = "Slide show", Value = "3"},
        //               new SelectListItem {Text = "Advertise", Value = "4"}
        //           };
        //        ViewBag.Type = listItemsList;
               
        //    }
        //    else
        //    {
        //       var listItemsList = new List<SelectListItem>
        //       {
        //           new SelectListItem {Text = "Logo", Value = "1", Selected = (1 == id)},
        //           new SelectListItem {Text = "Banner", Value = "2", Selected = (2 == id)},
        //           new SelectListItem {Text = "Slide show", Value = "3", Selected = (3 == id)},
        //           new SelectListItem {Text = "Advertise", Value = "4", Selected = (4 == id)}
        //       };
        //        ViewBag.Type = listItemsList;
        //    }
        //}

        //private void LoadLinkType(string target=null)
        //{
        //    var linkType = new List<SelectListItem>
        //    {
        //        new SelectListItem {Text = "_self", Value = "_self",Selected = target=="_self"},
        //        new SelectListItem {Text = "_blank", Value = "_blank",Selected = target=="_blank"},
        //        new SelectListItem {Text = "_parent", Value = "_parent",Selected = target=="_parent"},
        //        new SelectListItem {Text = "_top", Value = "_top",Selected = target=="_top"}
        //    };
        //    ViewBag.LinkType = linkType;
        //}
        //[HttpGet]
        //public ActionResult Add()
        //{
        //    LoadAdvertiseType();
        //    LoadLinkType();

        //    return View();
        //}

        //[HttpPost]
        //public ActionResult Add(FormCollection collection)
        //{
        //    try
        //    {
        //        var type = collection["Type"];
        //        LoadAdvertiseType(Convert.ToInt32(type));
        //        LoadLinkType();
        //        var name = collection["txtName"];
        //        if (string.IsNullOrEmpty(name))
        //        {
        //            Response.Write("<script>alert('Tên không được trống');</script>");
        //            return View();
        //        }
        //        var file = Request.Files["fufile"];
        //        if (file != null && string.IsNullOrEmpty(file.FileName))
        //        {
        //            Response.Write("<script>alert('hình ảnh không được trống');</script>");
        //            return View();
        //        }
        //        if (file != null)
        //        {
        //            var path = "Images/Advertise/" + file.FileName;
        //            file.SaveAs(Server.MapPath("~/" + path));
        //            var link = collection["txtLink"];
        //            if (string.IsNullOrEmpty(link))
        //            {
        //                Response.Write("<script>alert('liên kết không được trống');</script>");
        //                return View();
        //            }

        //            var advertise = new RelaxAdvertiseDataContract
        //            {
        //                Name = name,
        //                Image = path,
        //                Link = link,
        //                Target = collection["LinkType"],
        //                Decription = collection["txtDecription"],
        //                Ord = string.IsNullOrEmpty(collection["txtOrd"]) ? 1 : Convert.ToInt32(collection["txtOrd"]),
        //                Price = string.IsNullOrEmpty(collection["txtPrice"]) ? 0 : Convert.ToDouble(collection["txtPrice"]),
        //                Type = Convert.ToInt32(type),
        //                FromDate = string.IsNullOrEmpty(collection["txtFromDate"]) ? DateTime.Now : Convert.ToDateTime(collection["txtFromDate"]),
        //                ToDate = string.IsNullOrEmpty(collection["txtToDate"]) ? DateTime.Now : Convert.ToDateTime(collection["txtToDate"]),
        //                Active = collection["chkActive"].Split(',').Length > 1 ? 1 : 0
        //            };
        //            //var adId = _service1.AddAdvertise(advertise);
        //            //if (adId <= 0) return RedirectToAction("Add", "Advertise");

        //            //advertise.Id = adId;
        //            var lData = (List<RelaxAdvertiseDataContract>)_cache["Advertise"];
        //            if (lData == null)
        //                lData = new List<RelaxAdvertiseDataContract> { advertise };
        //            else
        //                lData.Add(advertise);

        //            _cache["Advertise"] = lData;
        //        }

        //        return RedirectToAction("Index", "Advertise");
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("<script>alert('chi tiết lỗi:" + ex.Message + "');</script>");
        //        return View();
        //    }
        //}

        //[HttpGet]
        //public ActionResult Edit(int id=0)
        //{
        //    try
        //    {
        //        var lData = (List<RelaxAdvertiseDataContract>)_cache["Advertise"];
        //        var first = lData.FirstOrDefault(p => p.Id == id);
        //        if (first == null)
        //            return HttpNotFound();

        //        LoadAdvertiseType(Convert.ToInt32(first.Type));
        //        LoadLinkType(first.Target);

        //        //return View(first);
        //        return View();
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("<script>alert('chi tiết lỗi:" + ex.Message + "');</script>");
        //        return RedirectToAction("Index","Advertise");
        //    }
        //}

        //[HttpPost]
        //public ActionResult Edit(FormCollection collection)
        //{
        //    try
        //    {
        //        var type = collection["Type"];
        //        LoadAdvertiseType(Convert.ToInt32(type));
        //        LoadLinkType();
        //        var name = collection["txtName"];
        //        if (string.IsNullOrEmpty(name))
        //        {
        //            Response.Write("<script>alert('Tên không được trống');</script>");
        //            return View();
        //        }
        //        var file = Request.Files["fufile"];

        //        var link = collection["txtLink"];
        //        if (string.IsNullOrEmpty(link))
        //        {
        //            Response.Write("<script>alert('liên kết không được trống');</script>");
        //            return View();
        //        }

        //        var lData = (List<RelaxAdvertiseDataContract>)_cache["Advertise"];
        //        var first = lData.FirstOrDefault(p => p.Id == Convert.ToInt32(collection["id"]));

        //        if (first != null)
        //        {
        //            var path = "";
        //            if (file != null && !string.IsNullOrEmpty(file.FileName))
        //            {
        //                path = "Images/Advertise/" + file.FileName;
        //                file.SaveAs(Server.MapPath("~/" + path));
        //                _utility.DeleteFile(Server.MapPath("~/" + first.Image));
        //            }
        //            else
        //                path = first.Image;

        //            first.Name = name;
        //            first.Image = path;
        //            first.Link = link;
        //            first.Target = collection["LinkType"];
        //            first.Decription = collection["txtDecription"];
        //            first.Ord = string.IsNullOrEmpty(collection["txtOrd"]) ? 1 : Convert.ToInt32(collection["txtOrd"]);
        //            first.Price = string.IsNullOrEmpty(collection["txtPrice"])
        //                ? 0
        //                : Convert.ToDouble(collection["txtPrice"]);
        //            first.Type = Convert.ToInt32(type);
        //            first.FromDate = string.IsNullOrEmpty(collection["txtFromDate"])
        //                ? DateTime.Now
        //                : Convert.ToDateTime(collection["txtFromDate"]);
        //            first.ToDate = string.IsNullOrEmpty(collection["txtToDate"])
        //                ? DateTime.Now
        //                : Convert.ToDateTime(collection["txtToDate"]);
        //            first.Active = collection["chkActive"].Split(',').Length > 1 ? 1 : 0;

        //            //var result = _service1.UpdateAdvertise(first);

        //            //if (!result) return View();
                    
        //            _cache["Advertise"] = lData;
        //        }

        //        return RedirectToAction("Index", "Advertise");
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("<script>alert('chi tiết lỗi:"+ex.Message+"');</script>");
        //        return View();
        //    }
            
        //}

        //[HttpGet]
        //public ActionResult Delete(int id)
        //{
        //    try
        //    {
        //        var lId = new List<int> { id };
        //        //var ok = _service1.DeleteAdvertise(lId);
        //        //if (ok)
        //        //{
        //        //    var lData = (List<RelaxAdvertiseDataContract>)_cache["Advertise"];
        //        //    var first = lData.FirstOrDefault(p => p.Id == id);
        //        //    if (first != null)
        //        //    {
        //        //        _utility.DeleteFile(Server.MapPath("~/" + first.Image));
        //        //        lData.Remove(first);
        //        //    }

        //        //    _cache["Advertise"] = lData;
        //        //}
        //        return RedirectToAction("Index", "Advertise");
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("<script>alert('chi tiết lỗi:" + ex.Message + "');</script>");
        //        return RedirectToAction("Index", "Advertise");
        //    }
            
        //}
      
    }
}
